<?php
/**
 * Desc:
 * User: 叶力铭
 */

class Certification
{
    //错误信息
    static protected $error = '';
    //错误码
    static protected $code = '';
    //默认请求头
    static private $headers = ['Content-type:application/json;charset=utf-8'];
    //
    static private $appKey = '44faf876aace429a8904d0ac';

    static private $masterSecret = 'f68803fa72d0b3c69c664778';

    static private $privateKey = FCPATH.'../'.'keys'.DIRECTORY_SEPARATOR.'jverification'.DIRECTORY_SEPARATOR.'private_key';

    /**
     * Desc: 获取错误信息
     * User: 叶力铭
     */
    static public function getErr()
    {
        return self::$error;
    }

    /**
     * Desc: 设置错误信息
     * User: 叶力铭
     * @param $msg
     */
    static private function setErr($msg)
    {
        self::$error = $msg;
    }

    /**
     * Desc: 获取错误信息
     * User: 叶力铭
     */
    static public function getCode()
    {
        return self::$code;
    }

    /**
     * Desc: 设置配置信息
     * User: 叶力铭
     * @param $appKey
     * @param $masterSecret
     * @param $privateKey
     */
    public function setConfig($appKey, $masterSecret, $privateKey)
    {
        self::$appKey = $appKey;
        self::$masterSecret = $masterSecret;
        self::$privateKey = $privateKey;
    }

    public function loginTokenVerify($loginToken, $exId = null)
    {
        $url = 'https://api.verification.jpush.cn/v1/web/loginTokenVerify';

        $data['loginToken'] = $loginToken;
        $data['exID'] = $exId;
        $headers = self::$headers;
        $headers[] = 'Authorization:Basic '.self::getAuthString();

        list($bool, $result) = self::httpPost($url, json_encode($data), $headers);

        if (!$bool){
            self::setErr('请求失败');
            return false;
        }

        if (8000 != $result['code']){
            self::setErr($result['content']);
            return false;
        }

        return self::phoneDecrypt($result['phone']);
    }

    /**
     * Desc: 数据解密
     * User: 叶力铭
     * @param string $phoneEncode
     * @return bool
     */
    private function phoneDecrypt(string $phoneEncode)
    {

        $bool = openssl_private_decrypt(
            base64_decode($phoneEncode),
            $data,
            openssl_pkey_get_private('file://'.self::$privateKey)
        );
        if (!$bool){
            self::setErr('解密失败');
            return false;
        }
        return $data;
    }

    /**
     * Desc: 组装
     * User: 叶力铭
     * @return string
     */
    private function getAuthString()
    {
        return base64_encode(self::$appKey.':'.self::$masterSecret);
    }

    /**
     * Desc: 发送post请求
     * User: 叶力铭
     * @param $url
     * @param $sendData
     * @param array $headers
     * @param int $second
     * @return array
     */
    private function httpPost($url, $sendData, $headers = [], $second = 60)
    {
        $ch = curl_init();

        if (!empty($headers)){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //设置header
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT,$second);
        //post提交方式
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData);
        //运行curl
        $data = curl_exec($ch);

        $dataArr = json_decode($data, true);
        if ($dataArr) {
            curl_close($ch);
            return [true, $dataArr];
        }else{
            $error = curl_errno($ch);
            curl_close($ch);
            return [false, $error];
        }
    }
}